<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #div1 {
            width: 100px;
            height: 100px;
            background-color: pink;
        }
    </style>
</head>

<body>
    <div id="div1"></div>
    <script>
        const div1 = document.querySelector('#div1');
        // 3s 把宽度从 100px 变为 640px 即增加540px
        // 60帧/s,3秒的话要变180帧，也即每次变化3px
        console.log(div1.offsetWidth);
        let currentWidth = 100;
        const maxWidth = 640;
        // setTimeout
        function animate() {
            var timer = setInterval(() => {
                currentWidth += 3;
                div1.style.width = currentWidth + 'px';
                if (maxWidth < currentWidth) {
                    clearInterval(timer);
                }
            }, 16.67);
        }
        animate();


        // RAF 
        function animate() {
            currentWidth += 3;
            div1.style.width = currentWidth + 'px';
            if (currentWidth < maxWidth) {
                window.requestAnimationFrame(animate);
                // 时间不用自己控制，网页会自动控制
                // 性能会优化
            }
        }
        animate();
    </script>
</body>

</html>